package com.ymatou.mq.api.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.mq.api.parameter.PublishBean;
import com.ymatou.mq.api.service.PublishCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 发送消息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Publish {
	private static PublishBean pb;
	private static PublishCall pc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送消息接口");
	}

	@Before
	public void caseUp() {
		pb = new PublishBean();
		pc = new PublishCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Publish_001() {
		Logger.start(true,"发送消息接口成功");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			pb.setAppid("test2");
			pb.setCode("gaoxu");
			pb.setIp("127.0.0.1");
			pb.setMsguniqueid("123");
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.verifyEquals(ECode.SUCCESS, pc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, pc.getMsg().toUpperCase(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Publish_002() {
		Logger.start(true,"不输入AppId,失败");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			// Logger.comment("setAppid:test2");
			// pb.setAppid("test2");
			pb.setCode("performancetest");
			pb.setIp("127.0.0.1");
			pb.setMsguniqueid(msguniqueid);
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.verifyEquals(ECode.ERROR400, pc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.MSG_APPID_NONE, pc.getMsg().toUpperCase(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Publish_003() {
		Logger.start(true,"不输入code,失败");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			pb.setAppid("test2");
			// Logger.comment("setCode:performancetest");
			// pb.setCode("performancetest");
			pb.setIp("127.0.0.1");
			pb.setMsguniqueid(msguniqueid);
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.verifyEquals(ECode.ERROR400, pc.getCode(), "验证Code");
			Logger.verifyEquals(ECode.MSG_CODE_NONE, pc.getMsg().toUpperCase(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Publish_004() {
		Logger.start(true,"输入错误的AppId,失败");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			pb.setAppid("errappid");
			pb.setCode("performancetest");
			pb.setIp("127.0.0.1");
			pb.setMsguniqueid(msguniqueid);
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.verifyIsContains("errappid没有对应的配置", pc.getReturnData(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Publish_005() {
		Logger.start(true,"输入错误的code,失败");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			pb.setAppid("test2");
			pb.setCode("errorcode");
			pb.setIp("127.0.0.1");
			pb.setMsguniqueid(msguniqueid);
			pb.setBody(body);
			pc.setData(pb);
			pc.callService();
			Logger.verifyIsContains("不存在对应的业务消息errorcode配置", pc.getReturnData(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_Publish_006() {
		Logger.start(true,"重复发送同一个Msguniqueid");
		try {
			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			for (int i = 0; i < 10000; i++) {
				pb = new PublishBean();
				pc = new PublishCall();
				pb.setAppid("smsproxy");
				// liguo uat_test1 mqbetatest
				pb.setCode("mqbetatest");
				pb.setIp("127.0.0.1");
				pb.setMsguniqueid(msguniqueid);
				pb.setBody(body);
				pc.setData(pb);
				pc.callService();
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Publish_007() {
		Logger.start(true,"发送多个消息观察");
		try {
			
			for (int i = 0; i < 100; i++) {
				String msguniqueid = TestDataManager.getRandomUUID().toString();
				pb = new PublishBean();
				pc = new PublishCall();
				pb.setAppid("test2");
				// liguo uat_test1 mqbetatest
				pb.setCode("gaoxu");
				pb.setIp("127.0.0.1");
				pb.setMsguniqueid(msguniqueid);
				pb.setBody("id:"+i+300);
				pc.setData(pb);
				pc.callService();
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}